import './arrangements.html';

import { Teams } from '/imports/api/teams/teams.js';
import { Areas } from '/imports/api/areas/areas.js';
import { Arrangements } from '/imports/api/arrangements/arrangements.js';


Template.arrangements.onCreated(function(){
  let self  = this;
  self.loadready = new ReactiveVar(false);
  self.team = new ReactiveVar();
  self.areas = new ReactiveVar();
  self.users = new ReactiveVar();
  self.users_load = new ReactiveVar(false);
  self.current_user = new ReactiveVar();
  self.arrangements = new ReactiveVar();
  self.newest_timeline = new ReactiveVar();

  Meteor.subscribe('current.team',{
    onReady: function(){
      self.loadready.set(true);
    }
  });


  self.autorun(function(){
    if (self.loadready.get()) {
      let team = Teams.findOne();
      self.team.set(team);
      Meteor.subscribe('team.users', self.team.get()._id, {
        onReady: function(){
          self.users_load.set(true)
        }
      });
      Meteor.subscribe('team.areas', self.team.get()._id, {
        onReady: function(){
          let areas = Areas.find({teamId: self.team.get()._id});
          self.areas.set(areas);
        }
      });
      Meteor.subscribe('team.arrangements', self.team.get()._id, {
        onReady: function(){
          let arrangements = Arrangements.find({teamId: self.team.get()._id});
          self.arrangements.set(arrangements);
        }
      })
    }
    if (self.users_load.get()) {
      let users = Meteor.users.find();
      self.users.set(users);
    }


  });

});

Template.arrangements.helpers({
  no_arranged_users(){
    return Meteor.users.find({isArranged: false});
  },
  current_user(){
    return Template.instance().current_user.get();
  },
  areas(){
    return Template.instance().areas.get();
  },

  areas_count(){
    let count = Template.instance().areas.get().count()+1;
    return  numberToWords.toWords(count);
  },
  area_width(){
    let count = Template.instance().areas.get().count()+1;
    let width= 1/count*100;
    return width;
  },
  arrangements(){
    return Template.instance().arrangements.get();
  },
  getRealName(userId){
    return Meteor.users.findOne({_id: userId}).realname;
  },
  getRandomColor(createdAt){
    return '#'+Math.floor(Math.random()*createdAt.getTime()).toString(16).slice(0,6);
  },
  timelines(){
    return [
      {
        begin: "6:00",
        end: "7:00"
      },
      {
        begin: "7:00",
        end: "8:00"
      },
      {
        begin: "8:00",
        end: "9:00"
      },
      {
        begin: "9:00",
        end: "10:00"
      },
      {
        begin: "10:00",
        end: "11:00"
      },
      {
        begin: "11:00",
        end: "12:00"
      },
      {
        begin: "12:00",
        end: "13:00"
      },
      {
        begin: "13:00",
        end: "14:30"
      },
      {
        begin: "14:00",
        end: "15:00"
      },
      {
        begin: "15:00",
        end: "16:00"
      },
      {
        begin: "16:00",
        end: "17:00"
      },
      {
        begin: "17:00",
        end: "18:00"
      },
      {
        begin: "19:00",
        end: "20:00"
      },
      {
        begin: "20:00",
        end: "21:00"
      },
      {
        begin: "21:00",
        end: "22:00"
      },
      {
        begin: "22:00",
        end: "23:00"
      },
      {
        begin: "23:00",
        end: "00:00"
      }
      ,
      {
        begin: "00:00",
        end: "01:00"
      }
      ,
      {
        begin: "01:00",
        end: "02:00"
      }
      ,
      {
        begin: "02:00",
        end: "03:00"
      }
      ,
      {
        begin: "03:00",
        end: "04:00"
      }
      ,
      {
        begin: "04:00",
        end: "05:00"
      }
      ,
      {
        begin: "05:00",
        end: "06:00"
      }
    ]
  },


  arrangements_area(areaId, begin, end, createdAt){
    let timeline_date_begin = createdAt;
    let index = begin.search(':');
    let hour = begin.slice(0, index);
    let min = begin.slice(index+1, begin.length);
    timeline_date_begin.setHours(hour,min,0,0);

    let timeline_date_end = createdAt;
    index = end.search(':');
    hour = end.slice(0, index);
    min = end.slice(index+1, end.length);
    timeline_date_end.setHours(hour,min,0,0);

    return Arrangements.find({
      areaId: areaId,
      $or: [{
        offTime:
        {
          $gte: new Date(timeline_date_begin.getTime()-3600000),
          $lte: new Date(timeline_date_end+3600000)
        }},{
        onTime:
        {
          $gte: new Date(timeline_date_begin.getTime()-3600000),
          $lte: new Date(timeline_date_end+3600000)
        }}
      ]

    });
  },
  parseTime(onTime){
    return moment(onTime).format("HH:mm");
  }

});
Template.arrangements.onRendered(function(){
    $(".ui.multiple.selection.dropdown").dropdown();
});

Template.arrangements.events({
  'click .no-arranged'(event, instance){
    event.stopPropagation();
    if ($(event.target).attr('data-id') == undefined) {
      return false;
    }
    let current_user = Meteor.users.findOne({_id: $(event.target).attr('data-id')});
    instance.current_user.set(current_user);
    $('.ui.modal').modal({
        closable  : true,
        onDeny    : function(){
         return true;
        },
        onApprove : function($element) {
          if ($element.attr('data-id') == undefined) {
            return false;
          }
          let userId = $(".userId").val();
          let areaId = $(".areaId").val();
          let onTime = $(".onTime").val();
          let offTime = $(".offTime").val();
          let dayOff = $(".dayOff").val();
          let teamId = $(".teamId").val();
          let on_time = Areas.findOne({_id: areaId}).createdAt;
          let index = onTime.search(':')
          let hour = onTime.slice(0, index);
          let min = onTime.slice(index+1, onTime.length);
          hour = parseInt(hour);
          min = parseInt(min);
          on_time.setHours(hour,min);
          let off_time = Areas.findOne({_id: areaId}).createdAt;
          index = offTime.search(':');
          hour = offTime.slice(1, index);
          min = offTime.slice(index+1, offTime.length);
          hour = parseInt(hour);
          min = parseInt(min);
          off_time.setHours(hour,min);

          Meteor.call("arrangements.insert",userId, areaId, on_time, off_time, dayOff, teamId, function(error){
            if (error) {
              alert('网络错误，请检查网络，或者刷新重试');
            }else{

            }
          })
        }
      }).modal('show');
  },

});
